# 获取按键信号 点亮灯泡
import RPi.GPIO as GP
import time

def control_01():
    red_pin = 11
    green_pin = 12
    btn_pin = 15
    GP.setmode(GP.BOARD)
    GP.setup(btn_pin,GP.IN)
    # GP.setup(15,GP.IN, pull_up_down=GP.PUD_DOWN)
    # GP.setup(15,GP.IN, pull_up_down=GP.PUD_UP)
    GP.setup((red_pin,green_pin),GP.OUT)
    GP.output(red_pin,0)  # red
    GP.output(green_pin,0)  # green
    global p_red
    p_red = GP.PWM(red_pin,200)
    global p_green
    p_green = GP.PWM(green_pin,200)
    dc_red = 0
    dc_green = 0
    p_red.start(dc_red)
    p_green.start(dc_green)
    while True:
        # button 按下:输入0 抬起:输入1
        # print(GP.input(15))
        if not GP.input(btn_pin): # 抬起 red
            # GP.output(green_pin,0)
            # GP.output(red_pin,1)
            dc_red+=10
            if dc_red>100:
                dc_red=0
            # if 1<dc_red<99:
            #     dc_red+=1
            # elif dc_red>99:
            #     dc_red-=1
            p_red.ChangeDutyCycle(dc_red)
            print(dc_red)
            time.sleep(1)

def control_02():
    GP.setmode(GP.BOARD)
    # GP.setup(15,GP.IN, pull_up_down=GP.PUD_DOWN)
    GP.setup(15,GP.IN, pull_up_down=GP.PUD_UP)  # 加上拉电阻, 目的是去除干扰
    GP.setup(11,GP.OUT)
    GP.output(11,0)

    
def close():
    p_red.stop()
    p_green.stop()
    GP.cleanup()


if __name__=='__main__':
    print(GP.BOTH)
    print(GP.RISING)
    try:
        control_01()
    except Exception as e:
        print(e)
        close()

